#!/bin/bash

# 风行融媒体客户端重新打包脚本
# 用于解决 NSIS 安装程序完整性检查失败问题

echo "=========================================="
echo "  风行融媒体 - 清理并重新打包"
echo "=========================================="
echo ""

# 1. 清理旧的构建文件
echo "🧹 清理旧的构建文件..."
rm -rf dist/
rm -rf node_modules/.cache/
echo "✅ 清理完成"
echo ""

# 2. 清理 electron-builder 缓存
echo "🧹 清理 electron-builder 缓存..."
rm -rf ~/Library/Caches/electron
rm -rf ~/Library/Caches/electron-builder
echo "✅ 缓存清理完成"
echo ""

# 3. 设置 Electron 镜像
echo "🔧 设置 Electron 镜像..."
export ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"
echo "✅ 镜像设置完成"
echo ""

# 4. 开始打包
echo "📦 开始打包 Windows 应用..."
echo "   这可能需要几分钟时间，请耐心等待..."
echo ""

npm run build:win

# 5. 检查打包结果
echo ""
echo "=========================================="
if [ -f "dist/风行融媒体 Setup 1.0.0.exe" ]; then
    echo "✅ 打包成功！"
    echo ""
    echo "生成的文件："
    echo "  📦 安装版: dist/风行融媒体 Setup 1.0.0.exe"
    if [ -f "dist/风行融媒体 1.0.0.exe" ]; then
        echo "  📦 便携版: dist/风行融媒体 1.0.0.exe"
    fi
    echo ""
    echo "文件大小："
    ls -lh dist/*.exe | awk '{print "  " $9 " - " $5}'
    echo ""
    echo "🎉 现在可以尝试安装了！"
else
    echo "❌ 打包失败，请查看上面的错误信息"
    exit 1
fi
echo "=========================================="
